home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
149_01
/
a685.c
< prev
next >
Wrap
Text File
|
1989-01-11
|
12KB
|
476 lines
/*
HEADER: CUG149;
TITLE: 6805 Cross-Assembler (Portable);
FILENAME: A685.C;
VERSION: 0.3;
DATE: 08/27/1988;
DESCRIPTION: "This program lets you use your computer to assemble
code for the Motorola 6805 family microprocessors.
The program is written in portable C rather than BDS
C. All assembler features are supported except
relocation linkage, and macros.";
KEYWORDS: Software Development, Assemblers, Cross-Assemblers,
Motorola, MC6805;
SYSTEM: CP/M-80, CP/M-86, HP-UX, MSDOS, PCDOS, QNIX;
COMPILERS: Aztec C86, Aztec CII, CI-C86, Eco-C, Eco-C88, HP-UX,
Lattice C, Microsoft C, QNIX C;
WARNINGS: "This program has compiled successfully on 2 UNIX
compilers, 5 MSDOS compilers, and 2 CP/M compilers.
A port to BDS C would be extremely difficult, but see
volume CUG113. A port to Toolworks C is untried."
AUTHORS: William C. Colley III;
*/
/*
6805 Cross-Assembler in Portable C
Copyright (c) 1985 William C. Colley, III
Revision History:
Ver Date Description
0.0 SEP 1985 Adapted from version 3.2 of the portable 6801 cross-
assembler. WCC3.
0.1 JUL 1986 Added compilation instructions and tweaks for CI-C86,
Eco-C88, and Lattice C. WCC3.
0.2 JAN 1987 Fixed bug that made "FCB 0," legal syntax. WCC3.
0.3 AUG 1988 Fixed a bug in the command line parser that puts it
into a VERY long loop if the user types a command line
like "A685 FILE.ASM -L". WCC3 per Alex Cameron.
This file contains the main program and line assembly routines for the
assembler. The main program parses the command line, feeds the source lines
to the line assembly routine, and sends the results to the listing and object
file output routines. It also coordinates the activities of everything. The
line assembly routines uses the expression analyzer and the lexical analyzer
to parse the source line and convert it into the object bytes that it
represents.
*/
/* Get global goodies: */
#include "a685.h"
/* Define global mailboxes for all modules: */
char errcode, line[MAXLINE + 1], title[MAXLINE];
int pass = 0;
int eject, filesp, forwd, listhex;
unsigned address, bytes, errors, listleft, obj[MAXLINE], pagelen, pc;
FILE *filestk[FILES], *source;
TOKEN token;
/* Mainline routine. This routine parses the command line, sets up */
/* the assembler at the beginning of each pass, feeds the source text */
/* to the line assembler, feeds the result to the listing and hex file */
/* drivers, and cleans everything up at the end of the run. */
static int done, ifsp, off;
void main(argc,argv)
int argc;
char **argv;
{
SCRATCH unsigned *o;
int newline();
void asm_line();
void lclose(), lopen(), lputs();
void hclose(), hopen(), hputc();
void error(), fatal_error(), warning();
printf("6805 Cross-Assembler (Portable) Ver 0.3\n");
printf("Copyright (c) 1985 William C. Colley, III\n\n");
while (--argc > 0) {
if (**++argv == '-') {
switch (toupper(*++*argv)) {
case 'L': if (!*++*argv) {
if (!--argc) { warning(NOLST); break; }
else ++argv;
}
lopen(*argv);
break;
case 'O': if (!*++*argv) {
if (!--argc) { warning(NOHEX); break; }
else ++argv;
}
hopen(*argv);
break;
default: warning(BADOPT);
}
}
else if (filestk[0]) warning(TWOASM);
else if (!(filestk[0] = fopen(*argv,"r"))) fatal_error(ASMOPEN);
}
if (!filestk[0]) fatal_error(NOASM);
while (++pass < 3) {
fseek(source = filestk[0],0L,0); done = off = FALSE;
errors = filesp = ifsp = pagelen = pc = 0; title[0] = '\0';
while (!done) {
errcode = ' ';
if (newline()) {
error('*');
strcpy(line,"\tEND\n");
done = eject = TRUE; listhex = FALSE;
bytes = 0;
}
else asm_line();
pc = word(pc + bytes);
if (pass == 2) {
lputs();
for (o = obj; bytes--; hputc(*o++));
}
}
}
fclose(filestk[0]); lclose(); hclose();
if (errors) printf("%d Error(s)\n",errors);
else printf("No Errors\n");
exit(errors);
}
/* Line assembly routine. This routine gets expressions and tokens */
/* from the source file using the expression evaluator and lexical */
/* analyzer, respectively. It fills a buffer with the machine code */
/* bytes and returns nothing. */
static char label[MAXLINE];
static int ifstack[IFDEPTH] = { ON };
static OPCODE *opcod;
void asm_line()
{
SCRATCH int i;
int isalph(), popc();
OPCODE *find_code(), *find_operator();
void do_label(), flush(), normal_op(), pseudo_op();
void error(), pops(), pushc(), trash();
address = pc; bytes = 0; eject = forwd = listhex = FALSE;
for (i = 0; i < BIGINST; obj[i++] = NOP);
label[0] = '\0';
if ((i = popc()) != ' ' && i != '\n') {
if (isalph(i)) {
pushc(i); pops(label);
if (find_operator(label)) { label[0] = '\0'; error('L'); }
}
else {
error('L');
while ((i = popc()) != ' ' && i != '\n');
}
}
trash(); opcod = NULL;
if ((i = popc()) != '\n') {
if (!isalph(i)) error('S');
else {
pushc(i); pops(token.sval);
if (!(opcod = find_code(token.sval))) error('O');
}
if (!opcod) { listhex = TRUE; bytes = BIGINST; }
}
if (opcod && opcod -> attr & ISIF) { if (label[0]) error('L'); }
else if (off) { listhex = FALSE; flush(); return; }
if (!opcod) { do_label(); flush(); }
else {
listhex = TRUE;
if (opcod -> attr & PSEUDO) pseudo_op();
else normal_op();
while ((i = popc()) != '\n') if (i != ' ') error('T');
}
source = filestk[filesp];
return;
}
static void flush()
{
while (popc() != '\n');
}
static void do_label()
{
SCRATCH SYMBOL *l;
SYMBOL *find_symbol(), *new_symbol();
void error();
if (label[0]) {
listhex = TRUE;
if (pass == 1) {
if (!((l = new_symbol(label)) -> attr)) {
l -> attr = FORWD + VAL;
l -> valu = pc;
}
}
else {
if (l = find_symbol(label)) {
l -> attr = VAL;
if (l -> valu != pc) error('M');
}
else error('P');
}
}
}
#define NONUM 0
#define NEEDBYTE 1
#define HAVENUM 2
static void normal_op()
{
SCRATCH unsigned attrib, operand;
unsigned expr();
TOKEN *lex();
void do_label(), error(), unlex();
do_label();
bytes = (attrib = opcod -> attr) & MAXBYTES;
obj[0] = opcod -> valu; operand = obj[1] = obj[2] = 0;
if (!(attrib & ~MAXBYTES)) return;
if (attrib & BIT) {
if ((operand = expr()) > 7) error('V');
else obj[0] += operand << 1;
operand = 0;
}
if ((lex() -> attr & TYPE) != SEP) {
if ((token.attr & TYPE) == IMM) {
if (attrib & IMMED) {
bytes = 2; obj[0] -= 0x10;
if ((operand = expr()) > 0xff && operand < 0xff80) error('V');
else obj[1] = low(operand);
}
else error('A');
return;
}
unlex(); operand = expr();
if ((attrib & (DIR + REL)) == (DIR + REL)) {
attrib &= ~DIR;
if (operand > 255) error('V');
else obj[1] = operand;
operand = expr();
}
if (attrib & REL) {
if ((operand = word(operand - (pc + bytes))) > 0x7f &&
operand < 0xff80) {
error('B'); operand = -bytes;
}
obj[bytes - 1] = low(operand);
return;
}
if ((token.attr & TYPE) == EOL) {
if ((attrib & EXT) && (forwd || operand > 0xff)) {
obj[0] += 0x10;
obj[1] = high(operand); obj[2] = low(operand);
}
else {
bytes = 2;
if (operand > 0xff) error('V');
else obj[1] = operand;
}
return;
}
}
if ((lex() -> attr & TYPE) != REG) error('S');
else {
if (!(attrib & IDX)) error('A');
else if ((attrib & EXT_IDX) && (forwd || operand > 0xff)) {
obj[0] += 0x20; obj[1] = high(operand); obj[2] = low(operand);
}
else if (forwd || operand) {
obj[0] += 0x30; bytes = 2;
if (operand > 0xff) error('V');
else obj[1] = low(operand);
}
else { obj[0] += 0x40; bytes = 1; }
}
return;
}
static